#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Author:Lijiacai
Email:1050518702@qq.com
===========================================
CopyRight@JackLee.com
===========================================
"""

import os
import sys
import json
import requests

cur_dir = os.path.split(os.path.realpath(__file__))[0]
sys.path.append("%s/.." % cur_dir)
import config


class ESSettings():
    def deal_url(self, url: str):
        return url.replace("://", "HTTPPTTH").replace("//", "/").replace("HTTPPTTH", "://")

    def set_max_result(self, es_host, es_index, max_=1000000, ):
        url = es_host + f"/{es_index}/_settings"
        url = self.deal_url(url=url)
        data = {
            "index": {"max_result_window": max_}
        }
        ret = requests.put(url, json=data).text
        print(ret)

    def set_mapping(self, es_host, es_index, mapping):
        url = es_host + f"/{es_index}"
        url = self.deal_url(url=url)
        data = mapping
        ret = requests.put(url, json=data).text
        print(ret)


class Run(ESSettings):
    app_order_dataset = {
        "mappings": {
            "doc": {
                "properties": {
                    "gettime":
                        {"type": "date", "format": "yyyy-MM-dd HH:mm:ss"},
                    "rettime":
                        {"type": "date", "format": "yyyy-MM-dd HH:mm:ss"}
                }}}}

    def run_set_max_result(self):
        self.set_max_result(config.es_cluster[0], es_index=config.app_order_dataset)

    def run_set_mapping(self):
        self.set_mapping(es_host=config.es_cluster[0], es_index=config.app_order_dataset,
                         mapping=self.app_order_dataset)

    def go(self):
        self.run_set_mapping()
        self.run_set_max_result()


if __name__ == '__main__':
    run = Run()
    run.go()
